package com.zyp.dao.imp;

import com.zyp.bean.Student;
import com.zyp.bean.TheClass;
import com.zyp.dao.TheClassDao;
import com.zyp.ubil.DriverUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class TheClassDaoImp implements TheClassDao {
    QueryRunner qr = null;

    @Override
    public int add(Object[] objects) {
        //增加新的班级
        Connection con = null;

        try {
            qr = new QueryRunner();
            //1.获取数据库连接
            con = DriverUtil.getConnection();
            String sql = "INSERT INTO theclass VALUES(?,?,?,?,?,?);";
            int update = qr.update(con, sql, objects);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭
            DriverUtil.close(con);
        }
        return 0;

    }

    @Override
    public int delete(String stirng) {
        qr = new QueryRunner(DriverUtil.getDataSource());
        //判断要删除的班级里面是否有学生
        String query = "select * from student where classID=?";
        try {
            List<Object[]> query1 = qr.query(query, new ArrayListHandler(), stirng);
            if ((query1.size()) != 0) {
                return 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql = "delete from theclass where classID=?";
        int update = 0;
        try {
            update = qr.update(sql, stirng);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }

    @Override
    public int update(Object[] objects) {
        qr = new QueryRunner(DriverUtil.getDataSource());
        String sql = "select * from theclass where theClassName=?";
        //存储不同的值
        Object[] storage = new Object[7];
        try {
            //sql查询数据库中是否存在要修改的id
            TheClass employee = qr.query(sql, new BeanHandler<TheClass>(TheClass.class), objects[1]);
            System.out.println(employee);
            //判断获取过来的值是否为空
            if (employee == null) {
                return 0;
            } else {
                //将获取到的值存储到数组中
                storage[0] = employee.getClassID();
                storage[1] = employee.getTheClassName();
                storage[2] = employee.getGrade();
                storage[3] = employee.getNameOfClassTeacher();
                storage[4] = employee.getClassMotto();
                storage[5] = employee.getClassSize();

                for (int i = 1; i < objects.length; i++) {
                    if (!objects[i].equals("") && !storage[i].equals(objects[i]) && !storage[i].equals("--")) {
                        storage[i] = objects[i];
                    }
                }
                storage[6] = employee.getClassID();

                for (Object o : storage) {
                    System.out.println(o);
                }
                sql = "update theclass set classID=?,theClassName=?,grade=?,nameOfClassTeacher=?,classMotto=?,classSize=? where classID=?";
                int update = qr.update(sql, storage);
                System.out.println(update);
                return update;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Object[]> pagingQuery(int i) {
        qr = new QueryRunner(DriverUtil.getDataSource());
        String sql = "select * from theclass limit ?,5";
        try {
            List<Object[]> query = qr.query(sql, new ArrayListHandler(), i);
            if (query != null) {
                return query;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
